package com.ucar.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * 圆图像
 * @author moon
 *
 */
@SuppressLint("DrawAllocation")
public class CircleImageView extends ImageView 
{
	private Paint mCirclePaint;
	public CircleImageView(Context context, AttributeSet attrs) 
	{
		super(context, attrs);
		
		init();
	}
	
	private void init()
	{
		mCirclePaint = new Paint();
		mCirclePaint.setAntiAlias( true );
	}
	
	@Override
	protected void onDraw(Canvas canvas) 
	{
		int width = getWidth();
		int height = getHeight();
		int centerX = width / 2;
		int centerY = height / 2;
		int radius = ( width > height ? height : width ) / 2;
		
		Bitmap mBitmap = Bitmap.createBitmap( width, height, Config.ARGB_8888 );
		Canvas canvasTemp = new Canvas( mBitmap );
		
		canvasTemp.drawCircle( centerX, centerY, radius, mCirclePaint );
		
		mCirclePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
		
		BitmapDrawable mBitmapDrawable = (BitmapDrawable)getDrawable();
		canvasTemp.drawBitmap( mBitmapDrawable.getBitmap(), 0, 0, mCirclePaint );

		canvas.drawBitmap( mBitmap, 0, 0, null );
		
		mCirclePaint.setXfermode( null );
	}
}
